﻿Vue.createApp({
    data() {
        return {
            type: 1,
            form: {
                phone: {
                    Phone: "",
                    SmsCode: "",
                },
                account: {
                    Account: "",
                    Password: "",
                    captchaCode: ""
                }
            },
            captcha: "",
        }
    },
    mounted: function () {
        this.GetCaptcha();
    },
    computed: function () {

    },
    methods: {
        GetCaptcha: function () {
            var that = this;
            lg.request({
                url: "/Base/GetImgCaptcha",
                success: function (d) {
                    if (d.code == 0) {
                        that.captcha = d.data
                        console.log(that.captcha)
                    }
                }
            })
        },
        GetSmsCode: function (e) {
            var phone = this.form.phone.Phone
            var b = lg.regx.phone(phone);
            var bt = $(e.target)
            var timer = 60;
            if (!b) {
                layer.msg("请填写有效的手机号码!", { icon: 2 });
                return false;
            } else {
                bt.prop("disabled", true).text("已发送(" + timer + ")");
                setInterval(function () {
                    timer--;
                    if (timer > 0) {
                        bt.text("已发送(" + timer + ")")
                    } else {
                        bt.prop("disabled", false).text("获取验证码");
                    }
                }, 1000)
                lg.request({
                    url: "/Base/GetSmsCode",
                    method: "post",
                    data: { Phone: phone },
                    success: function (d) {
                        if (d.code == 0) {
                            layer.msg("短信已发送!收到后请尽快使用!", {icon:1})
                        } else {
                            layer.msg(d.msg, {icon:2})
                        }
                    },
                    fail: function (d) {
                        layer.msg(d, { icon: 2 })
                        bt.prop("disabled", false).text("获取验证码");
                    }
                })
            }
        },
        LoginType: function (type) {
            this.type = type;
        },  
        Login: function () {
            var url, data;
            if (this.type == 1) {
                url = "/Base/LoginByAccount";
                data = this.form.account
            } else if (this.type == 2) {
                url = "/Base/LoginByPhone";
                data = this.form.phone
            }
            if (url && data) {
                lg.request({
                    url: url,
                    method:"post",
                    data: data,
                    success: res=> {
                        if (res.code == 0) {
                            top.location.href = "/Lgm"
                        } else {
                            this.GetCaptcha();
                            layer.msg(res.msg, {icon:2})
                        }
                    }
                })
            }
        }
    },  
}).mount("#lg-login")